package com.alan.wifishare.presenter;

import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.ConnectedDeviceInfo;
import com.alan.libapi.model.business.DeviceStatisticsInfo;
import com.alan.libapi.model.business.SharedWifiInfo;
import com.alan.libapi.model.business.TcpResponseStatus;
import com.alan.library.allog.KLog;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.view.iview.IVCNTHaveConnected;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: CNTHaveConnectedPS
 * @describtion: CNTHaveConnectedPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/27 00:40
 */
public class CNTHaveConnectedPS {

    private IVCNTHaveConnected ivcntHaveConnected;

    public CNTHaveConnectedPS(IVCNTHaveConnected ivcntHaveConnected) {
        this.ivcntHaveConnected = ivcntHaveConnected;
    }


    /**
     * 向Link设备发送用户Id
     */
    public void sendMasterInfo(){
        if(GlobalField.businessTcp.isAlive()){
            GlobalField.businessTcp.sendMasterInfo(GlobalField.userSession.getUserId(), new CallbackListener<TcpResponseStatus>() {
                @Override
                public void onSuccess(TcpResponseStatus result) {
                    KLog.e(result);
                    if(result != null){
                        GlobalField.handler.post(new Runnable() {
                            @Override
                            public void run() {
//                            sendWifiInfo();
//                            getConnectedDevicesList();
                            }
                        });
                    }
                }
                @Override
                public void onFailure(int errorCode, String msg) {
                    KLog.e(msg);
                    GlobalField.handler.post(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });
                }
            });
        }else {
            KLog.e("Socket未连接");
        }
    }

    /**
     * 发送wifiList给Link设备
     */
    public void sendWifiInfo(){
        final List<SharedWifiInfo> wifiInfoArrayList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            SharedWifiInfo sharedWifiInfo = new SharedWifiInfo();
            sharedWifiInfo.setWifiId(234);
            sharedWifiInfo.setWifiMasterId(6);
            sharedWifiInfo.setWifiStatus(1);
            sharedWifiInfo.setWifiSSID("VFBMaW5rXzM4ODY");
            sharedWifiInfo.setWifiPassword("aWxvdmV5b3U=");
            sharedWifiInfo.setWifiMac("1FC12D156E78F730B84C539B27B4796B");
            sharedWifiInfo.setWifiLongitude(113.921126);
            sharedWifiInfo.setWifiLatitude(22.589214);
            sharedWifiInfo.setWifiAreaId(2);
            sharedWifiInfo.setWifiAddress("广东省深圳市宝安71区");
            wifiInfoArrayList.add(sharedWifiInfo);
        }
        GlobalField.sharedWifiInfoList = wifiInfoArrayList;
        if(GlobalField.businessTcp.isAlive()){
            GlobalField.businessTcp.sendWifiInfo(GlobalField.userSession.getUserId(), GlobalField.sharedWifiInfoList, new CallbackListener<TcpResponseStatus>() {
                @Override
                public void onSuccess(TcpResponseStatus result) {
                    KLog.e(result);
                    if(result != null){
                        GlobalField.handler.post(new Runnable() {
                            @Override
                            public void run() {

                            }
                        });
                    }
                }
                @Override
                public void onFailure(int errorCode, String msg) {
                    KLog.e(msg);
                    GlobalField.handler.post(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });
                }
            });

        }else {
            KLog.e("Socket未连接");
        }
    }




    public void getDeviceStatisticsInfo(){
        // TODO: Alan 从Link获取设备的统计信息
//        DeviceStatisticsInfo deviceStatisticsInfo = new DeviceStatisticsInfo();
//        deviceStatisticsInfo.setGpm(1024*1024*100);
//        deviceStatisticsInfo.setNetSpeed(1024*1024);
//        ivcntHaveConnected.showDeviceStatisticsInfo(deviceStatisticsInfo);

        GlobalField.businessTcp.modifyDeviceWifi(9, "VFBMaW5rXzM4ODY", "aWxvdmV5b3U", new CallbackListener<TcpResponseStatus>() {
            @Override
            public void onSuccess(TcpResponseStatus result) {
                KLog.e(result);
                if(result != null){
                    GlobalField.handler.post(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                KLog.e(msg);
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        });
    }
    public boolean mDebug = true;
    public void getConnectedDevicesList(){
        // TODO: Alan 从Link获取已连接的设备列表

        if(mDebug){
            List<ConnectedDeviceInfo> connectedDevicesList = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                ConnectedDeviceInfo connectedDeviceInfo = new ConnectedDeviceInfo();
                connectedDeviceInfo.setDeviceName("Coolpad_F2"+i);
                connectedDeviceInfo.setDeviceMac("3D4F2C3F7CC"+i);
                connectedDevicesList.add(connectedDeviceInfo);
            }
            ivcntHaveConnected.showConnectedDevicesList(connectedDevicesList);
            return;
        }
        if(GlobalField.businessTcp.isAlive()){
            GlobalField.businessTcp.getDeviceUserList(GlobalField.userSession.getUserId(), new CallbackListener<List<ConnectedDeviceInfo>>() {
                @Override
                public void onSuccess(List<ConnectedDeviceInfo> result) {
                    KLog.e(result);
                    if(result != null){
                        GlobalField.handler.post(new Runnable() {
                            @Override
                            public void run() {

                            }
                        });
                    }
                }

                @Override
                public void onFailure(int errorCode, String msg) {
                    KLog.e(msg);
                    GlobalField.handler.post(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });
                }
            });
        }else {
            KLog.e("Socket未连接");
        }
    }

    public void deleteDeviceUser(String userMac){
        if(GlobalField.businessTcp.isAlive()){
            GlobalField.businessTcp.deleteDeviceUser(GlobalField.userSession.getUserId(), userMac, new CallbackListener<TcpResponseStatus>() {
                @Override
                public void onSuccess(TcpResponseStatus result) {
                    KLog.e(result);
                    if(result != null){
                        GlobalField.handler.post(new Runnable() {
                            @Override
                            public void run() {

                            }
                        });
                    }
                }

                @Override
                public void onFailure(int errorCode, String msg) {
                    KLog.e(msg);
                    GlobalField.handler.post(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });
                }
            });
        }else {
            KLog.e("Socket未连接");
        }
    }
}
